package com.itzyf.pokemondata.activity

import android.app.Activity
import android.app.DatePickerDialog
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbDrillDo
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_edit_drill.*
import org.litepal.LitePal
import org.litepal.extension.findFirst
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author 依风听雨
 * @version 创建时间：2019/02/06 21:05
 */
class EditDrillActivity : BaseActivity() {
    private val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE)
    override fun handlerActivity() {
        val drillId = intent.getLongExtra("drillId", -1L)

        val title = if (drillId != -1L) "编辑存钻记录" else "添加存钻记录"
        setTitle(title)

        if (drillId != -1L) {
            val dbDrillDo = LitePal.find(DbDrillDo::class.java, drillId)
            etRecord.setText(dbDrillDo.record.toString())
            tvDate.text = sdf.format(dbDrillDo.date)
        } else {
            tvDate.text = sdf.format(Date())
        }

        btnSubmit.setOnClickListener {
            val record = etRecord.text.toString()
            if (record.isEmpty()) {
                showToast("请输入当前钻石数量")
                return@setOnClickListener
            }
            val dbDrillDo = DbDrillDo(sdf.parse(tvDate.text.toString()), record.toLong())
            if (drillId != -1L) {
                dbDrillDo.update(drillId)
            } else {
                val drillDo = LitePal.where("date = ?", dbDrillDo.date.time.toString())
                    .findFirst(DbDrillDo::class.java)
                if (drillDo != null) {
                    showToast("当前日期已有记录")
                    return@setOnClickListener
                }
                dbDrillDo.save()
            }
            setResult(Activity.RESULT_OK)
            finish()

        }

        tvDate.setOnClickListener {
            val date = sdf.parse(tvDate.text.toString())
            val ca = Calendar.getInstance()
            ca.time = date
            val mYear = ca.get(Calendar.YEAR)
            val mMonth = ca.get(Calendar.MONTH)
            val mDay = ca.get(Calendar.DAY_OF_MONTH)

            DatePickerDialog(
                this@EditDrillActivity,
                DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
                    val calendar = Calendar.getInstance()
                    calendar.set(Calendar.YEAR, year)
                    calendar.set(Calendar.MONTH, month)
                    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
                    tvDate.text = sdf.format(calendar.time)
                }, mYear, mMonth, mDay
            ).show()
        }

    }

    override fun getContentViewId() = R.layout.activity_edit_drill

    override fun getToolbarTitle() = ""

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        return if (intent.getLongExtra("drillId", -1L) == -1L) {
            super.onCreateOptionsMenu(menu)
        } else {
            menuInflater.inflate(R.menu.delete, menu)
            true
        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.action_delete) {
            val builder = QMUIDialog.MessageDialogBuilder(this)
                .setMessage("是否删除该记录？")
                .setTitle("提示")
                .addAction("取消") { dialog, _ ->
                    dialog.dismiss()
                }
                .addAction(
                    "确定"
                ) { dialog, _ ->
                    val drillId = intent.getLongExtra("drillId", -1L)
                    LitePal.delete(DbDrillDo::class.java, drillId)
                    dialog.dismiss()

                    setResult(Activity.RESULT_OK)
                    finish()
                }
            builder.show()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}